<template>
  <a-drawer
    :title="titleMap"
    :width="550"
    :visible.sync="visible"
    :body-style="{ 'padding-top': '0px', paddingBottom: '80px' }"
    @close="close"
  >
    <a-form
      :form="form"
      :label-col="{ span: 6 }"
      :wrapper-col="{ span: 18 }"
      layout="horizontal"
    >
      <a-form-item :label="$t('helptitle')">
        <br />
        <a-input
          v-decorator="[
            'ConfigurationItemsContent',
            { rules: [{ required: true, message: $t('helptext.v1') }] },
          ]"
          :placeholder="$t('helptext.v1')"
          style="width: 400px; margin-left: -95px"
        />
      </a-form-item>
      <a-form-item :label="$t('helptitle')">
        <br />
        <a-input
          v-decorator="[
            'ConfigurationItemsContentEn',
            { rules: [{ required: true, message: $t('helptext.v2') }] },
          ]"
          :placeholder="$t('helptext.v2')"
          style="width: 400px; margin-left: -95px"
        />
      </a-form-item>
      <a-form-item :label="$t('help.URL')">
        <br />

        <a-input
          v-decorator="[
            'ConfigurationItemsAnnotation',
            { rules: [{ required: true, message: $t('help.URL') }] },
          ]"
          :placeholder="$t('help.URL')"
          style="width: 400px; margin-left: -95px"
          :disabled="true"
        />
        <a-upload
          v-if="!urltxt"
          name="file"
          :multiple="true"
          :customRequest="uploadImage1"
        >
          <a-button>
            <a-icon type="upload" />
            {{ $t("user.verification-code.required111566163") }}
          </a-button>
        </a-upload>
      </a-form-item>
    </a-form>
    <div
      class="absolute right-0 bottom-0 w-full px-4 py-[10px] bg-white text-left z-1 flex justify-center"
    >
      <a-row class="w-full" :gutter="24">
        <a-col :span="12">
          <a-button
            class="w-full border-none hover:bg-[#3F8CFF] hover:bg-opacity-5 shadow"
            @click="close"
            >{{ $t("app.setting.themecolor.rr") }}</a-button
          >
        </a-col>
        <a-col :span="12">
          <a-button
            :loading="loading"
            class="w-full border-none shadow"
            type="primary"
            @click.native.prevent="submit()"
          >
            <span v-if="!loading">{{
              $t("user.verification-code.required111566165")
            }}</span>
            <span v-else>{{ $t("app.setting.themecolor.tt") }}...</span>
          </a-button>
        </a-col>
      </a-row>
    </div>
  </a-drawer>
</template>

<script>
import { configurationCreateUpdate } from "@/api/configs";
import axios from "axios";
import storage from "store";
import { ACCESS_TOKEN } from "@/store/mutation-types";

export default {
  data() {
    return {
      // 下面这些都是通用的
      form: this.$form.createForm(this),
      visible: false,
      loading: false,
      // 这个要根据服务修改title
      titleMap: "",
      env: process.env.VUE_APP_BASE_API,
      mode: 0,
      urltxt: "",
      fileName: ""
    };
  },
  mounted() {},
  methods: {
    // 上传文件
    uploadImage1(e) {
      let formdata = new FormData();
      formdata.append("file", e.file);
      axios({
        url: `${this.env}/v1/oss/upload/mp4`,
        method: "post",
        data: formdata,
        headers: {
          token: storage.get(ACCESS_TOKEN),
          tokens: storage.get("bmwToken") || "",
        },
      }).then(({ status, data }) => {
        if (status == 200 && data.code == 1) {
          // console.log(project);
          let txt = data.Url + data.data;
          this.urltxt = txt.replace(/\//g, "\\");
          //修改的时候需要给form赋值
          this.form.setFieldsValue({
            ConfigurationItemsAnnotation: this.urltxt,
          });
          this.fileName = e.file.name
          return true;
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    /**
     *
     * @param {*} type 0 新增 1 修改
     * @param {*} data 列表传过来的值
     */
    async open(mode = 0, data = {}) {
      this.titleMap = this.$t("addhelptxt");
      this.mode = mode;
      this.visible = true;
      await this.$nextTick();
      // 如果是修改
      if (mode == 1) {
        this.titleMap = this.$t("changehelptxt");
        //修改的时候需要给form赋值
        this.form.setFieldsValue({
          ConfigurationItemsContent: data.ConfigurationItemsContent,
          ConfigurationItemsAnnotation: data.ConfigurationItemsAnnotation,
          ConfigurationItemsContentEn: data.ConfigurationItemsContentEn,
          ConfigurationItemsAnnotationEn: data.ConfigurationItemsAnnotationEn,
          Id: data.Id,
        });
      } else {
        this.form.setFieldsValue({});
      }
      this.urltxt = "";
    },
    // 提交 通用的
    submit() {
      const {
        form: { validateFields },
      } = this;
      // 按钮loading
      this.loading = true;
      // validateFields 是校验form表单数据
      // force: true 是默认所有字段都校验
      // values 表单的值
      validateFields({ force: true }, async (err, values) => {
        if (!err) {
          try {
            const params = { ...values };
            // 标记为帮助列表数据
            params.ConfigurationItemsState = 13;
            await configurationCreateUpdate({
              ...params,
              ConfigurationItemsAnnotationEn: this.fileName
            });
            // 如果成功就调用父页面当前组件的succeed上绑定的事件
            this.$emit("succeed");
            this.$message.success(
              this.$t("user.verification-code.required111566110577308")
            );
            //如果成功就关闭
            this.close();
          } catch (error) {
            this.$message.error(error);
          } finally {
            this.loading = false;
          }
        } else {
          setTimeout(() => {
            this.loading = false;
          }, 600);
        }
      });
    },
    // 关闭 通用的
    close() {
      // 修改侧边栏状态
      this.visible = false;
      // 清空form的值
      this.form.resetFields();
    },
  },
};
</script>
<style lang="less" scoped>
/deep/.ant-form-item-label {
  /* overflow: hidden; */
}
</style>
